
import basePanleScript from "../../base/basePanleScript";
import gameModel from "./gameModel";
import AudioUtils from "../AudioUtils";

const { ccclass, property } = cc._decorator;
const point=[cc.v2(115,141),cc.v2(68,-165),cc.v2(-173,25)];
@ccclass
export default class aloneGame extends basePanleScript {
    public onLoaded(param: any): void {
        
    }
    public onEnter(param: any): void {
        
    }
    public onRefresh(param: any): void {
        
    }
    private outsideTrianglePool: any;
    private outSideListClo:any;
    private outsideTriangle:any;

    private rolePosNum:number;
    // private role:any

    @property(cc.Node)
    role:cc.Node=null;

    //创造缺口三角形
    createOutsideTriangle(speed,initScale){
        if(gameModel.getInstance().isGameOver){
            return;
        }
        if(!this.outsideTriangle){
            console.error("预制件不存在")
            return;
        }
        let enemy = null;
        if (this.outsideTrianglePool.size() > 0) { // 通过 size 接口判断对象池中是否有空闲的对象
            enemy = this.outsideTrianglePool.get();
        } else { // 如果没有空闲对象，也就是对象池中备用对象不够时，我们就用 cc.instantiate 重新创建
            enemy = cc.instantiate(this.outsideTriangle);
        }
        enemy.parent = this.outSideListClo; // 将生成的敌人加入节点树
        enemy.setPosition(0,0);
        var randomNum=Math.floor(Math.random()*3);
        enemy.rotation = 120*randomNum;
        enemy.getComponent('outSideTrianleItem').init(speed,initScale); //接下来就可以调用 enemy 身上的脚本进行初始化
    }
    switchEvent(event){
        if(gameModel.getInstance().isGameOver) return;
        this.rolePosNum++;
        this.rolePosNum=this.rolePosNum%point.length;
        // this.node.emit('getScore');
        console.error("this.rolePosNum",this.rolePosNum); 
        AudioUtils.getInstance().playEffect("ball.wav")
        this.role.setPosition(point[this.rolePosNum]);
    }
    handler(data){
        // console.log("事件触发",data)
        switch(data.type){
            case "getScore":
                console.log("得分",data.content,this._get_("scoreLabel",cc.Label));
                this._get_("scoreLabel",cc.Label).string=data.content;
            break;
        }
    }

    onEnable(){
        AudioUtils.getInstance().playBgMusic("Music_Intro.mp3");
        this.outsideTrianglePool=gameModel.getInstance().outsideTrianglePool;
        this.outsideTriangle=gameModel.getInstance().outSideTrianlePrefab
        this.outSideListClo=this._get_("outSideListClo");
        this.rolePosNum=0;
        this._get_("scoreLabel",cc.Label).string=""
        this.role.setPosition(point[this.rolePosNum])
        this.node.on(cc.Node.EventType.TOUCH_START,this.switchEvent,this,true)
        this.schedule(function(){
            this.createOutsideTriangle(6,1);
        },1);
        gameModel.getInstance().isGameOver=false
        gameModel.getInstance().Score=0;
        (<any>window).CustomEventManager.bind("getScore",this);

    }
    onDisable(){
        console.error("aloneGame被销毁了吗")
        this.node.off(cc.Node.EventType.TOUCH_START,this.switchEvent,this,true);
        (<any>window).CustomEventManager.unBind("getScore",this);
    }
}
